# Contributor: Jacek Migacz <jacekmigacz@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=lvm2
pkgver=2.02.187
pkgrel=2
pkgdesc="Logical Volume Manager 2 utilities"
url="https://sourceware.org/lvm2/"
arch="all"
options="!check"  # Test tries to use real /dev/mapper/control.
license="GPL-2.0-only LGPL-2.1-only"
# lvm2 will dlopen libs so we need to add explicit depend
depends="lvm2-libs=$pkgver-r$pkgrel"
depends_dev="linux-headers"
makedepends_build="coreutils"
makedepends_host="$depends_dev libaio-dev util-linux-dev"
makedepends="$makedepends_build $makedepends_host"
source="https://mirrors.kernel.org/sourceware/lvm2/LVM2.$pkgver.tgz
	fix-stdio-usage.patch
	mallinfo.patch
	library_dir-default-config.patch
	mlockall-default-config.patch
	lvm.initd
	lvm.confd
	dmeventd.initd
	"
subpackages="
	$pkgname-static:lvm2_static
	device-mapper-static:dm_static
	device-mapper-libs:dm_libs
	device-mapper-event-static:dm_event_static
	device-mapper-event-libs:dm_event_libs
	$pkgname-dev
	$pkgname-doc
	$pkgname-dmeventd
	$pkgname-openrc
	device-mapper:dm
	device-mapper-udev:udev:noarch
	$pkgname-libs
	$pkgname-extra::noarch
	"

builddir="$srcdir/LVM2.$pkgver"

prepare() {
	default_prepare
	update_config_sub
}

build() {
	# during cross-compilation malloc test goes wrong
	export ac_cv_func_malloc_0_nonnull=yes
	export ac_cv_func_realloc_0_nonnull=yes

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--libdir=/lib \
		--sbindir=/sbin \
		--localstatedir=/var \
		--enable-static_link \
		--disable-nls \
		--disable-readline \
		--enable-pkgconfig \
		--enable-applib \
		--with-thin=internal \
		--enable-dmeventd \
		--enable-cmdlib \
		--with-thin-check=/sbin/thin_check \
		--with-thin-dump=/sbin/thin_dump \
		--with-thin-repair=/sbin/thin_repair \
		--with-dmeventd-path=/sbin/dmeventd \
		--enable-udev_rules \
		CLDFLAGS="$LDFLAGS"
	make
}

package() {
	make DESTDIR="$pkgdir" install
	install -d "$pkgdir"/etc/lvm/archive "$pkgdir"/etc/lvm/backup
	install -Dm755 "$srcdir"/lvm.initd "$pkgdir"/etc/init.d/lvm
	install -Dm644 "$srcdir"/lvm.confd "$pkgdir"/etc/conf.d/lvm
	ln -s libdevmapper.so.1.02 "$pkgdir"/lib/libdevmapper.so
}

dmeventd() {
	pkgdesc="Device-mapper event daemon"
	mkdir -p "$subpkgdir"/sbin
	mv "$pkgdir"/sbin/dmeventd "$subpkgdir"/sbin/
	install -Dm755 "$srcdir"/dmeventd.initd "$subpkgdir"/etc/init.d/dmeventd
}

dm_static() {
	pkgdesc="Device-mapper static library"
	mkdir -p "$subpkgdir"/lib
	mv "$pkgdir"/usr/lib/libdevmapper.a "$subpkgdir"/lib/
}

dm() {
	pkgdesc="Device mapper userspace library and tools from LVM2"
	mkdir -p "$subpkgdir"/sbin
	mv "$pkgdir"/sbin/dm* "$subpkgdir"/sbin/

	mkdir -p "$subpkgdir"/usr/sbin
	mv "$pkgdir"/usr/sbin/dm* "$subpkgdir"/usr/sbin/

	# remove duplicate dmstats
	rm -f "$subpkgdir"/usr/sbin/dmstats
}

udev() {
	pkgdesc="Device mapper udev rules"
	install="$subpkgname.post-install $subpkgname.post-upgrade"
	install_if="device-mapper eudev"
	mkdir -p "$subpkgdir"/usr/lib/udev
	mv "$pkgdir"/usr/lib/udev/rules.d "$subpkgdir"/usr/lib/udev/
}

libs() {
	pkgdesc="LVM2 shared libraries"
	depends=
	mkdir -p "$subpkgdir"/lib
	mv "$pkgdir"/lib/liblvm2*.so.* "$subpkgdir"/lib/
	mv "$pkgdir"/lib/libdevmapper-event-lvm2*.so.* "$subpkgdir"/lib/
	mv "$pkgdir"/lib/device-mapper "$subpkgdir"/lib/
}

dm_libs() {
	pkgdesc="Device-mapper shared library"
	depends=
	mkdir -p "$subpkgdir"/lib
	mv "$pkgdir"/lib/libdevmapper.so.* "$subpkgdir"/lib/
}

dm_event_libs() {
	pkgdesc="Device-mapper event daemon shared library"
	depends=
	mkdir -p "$subpkgdir"/lib
	mv "$pkgdir"/lib/libdevmapper-event.so.* "$subpkgdir"/lib/
}

dm_event_static() {
	pkgdesc="Device-mapper event daemon static library"
	mkdir -p "$subpkgdir"/lib
	mv "$pkgdir"/usr/lib/libdevmapper-event.a "$subpkgdir"/lib/
}

lvm2_static() {
	pkgdesc="$pkgdesc static library"
	mkdir -p "$subpkgdir"/lib
	mv "$pkgdir"/usr/lib/liblvm2*.a "$subpkgdir"/lib/
}

extra() {
	pkgdesc="LVM2 extra utilities"
	depends="bash coreutils"
	local util
	for util in sbin/lvmdump sbin/lvmconf sbin/fsadm sbin/blkdeactivate; do
		mkdir -p "$subpkgdir/${util%/*}"
		mv $pkgdir/$util $subpkgdir/$util
	done
}

sha512sums="3ce56f0c0d2e7dbcdae5be263199f73ee5c4c052599d53cde9b16500a326d0340893344f4671839be06b5b4225d592175d01c9974db14f2dc220d6e9a1d47e91  LVM2.2.02.187.tgz
9ad38a68620986db826ccbc4972c44b3ef12e5b37f1f573db20748e7b2327726c7c51b04aaad2a2e9bb76101ec352b7e7a102fdc4a75e79e9587cfc3b87da0e5  fix-stdio-usage.patch
9272ec8c5184ef5dc776ead8f74132e072b7563b5119a3a38b712f00d92a1e3878c9b3a54eb2b01dcba038110c686b39d4c17ecd0eb258537e9217d7ed03c408  mallinfo.patch
33004594a5cce7af1e493a2a66ce8ad2f864dde3b2ec0960d0fda9df9b7bcf65d9455f9eccf7860f48726c48f14111b843b0a0c9bd395ef2952b41aaab8092b2  library_dir-default-config.patch
e113c518bb7f03feea892252518f1df3733fad32f04ed0701a8313cf35ba932c3e2d29ecd97d739f8908b481ed1f6a2eb0129cb307214d0a084d9a8d0b4bc290  mlockall-default-config.patch
a853078660fd2fd943538924f56e81dc5793294e26b8f61d93e6188893f15f4a438d33792b341c1865d61e03f4a371b7c7ee0db5f4130ef7cb7aeaeb9290086a  lvm.initd
07caf8fa942290f3c953cc2463aaf55bac01d0bcb9351daf3880fa4d0eefb67fe00761c46a7a4da91cd2f8e492a12fed35853a15dc939cd80d19d3303bc3951d  lvm.confd
ca06220065525e93347efb7a5746a367cf55cb03574e50eed29bd7c4eae036bdd199b52c42d81811b6e742649e73ab8e66525f663bed66f9d1d82d0efde50332  dmeventd.initd"
